
import 'package:ble2025_plugin/ble2203_plugin.dart';
import 'package:ble2025_plugin_example/util/app_all_value.dart';
import 'package:ble2025_plugin_example/util/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

///社交提醒
class SocialDistancePage extends StatefulWidget {
  const SocialDistancePage({Key? key}) : super(key: key);

  @override
  State<SocialDistancePage> createState() => _SocialDistancePageState();
}

class _SocialDistancePageState extends State<SocialDistancePage> {


  List<String> intervalList = [];
  List<String> durationList = [];

  String interval = '1';
  String duration = '20';

  AppAllValueController controller = Get.find();

  @override
  void initState() {
    super.initState();
    for(int i = 1;i <= 250; i++){
      intervalList.add(i.toString());
    }
    for(int i = 20;i <= 60; i++){
      durationList.add(i.toString());
    }
    EventBus().on('dataCallBack', (arg) {
      Map maps = arg;
      switch(maps[DeviceKey.DataType] as String){
        case BleConst.SocialdistanceSetting:
        case BleConst.SocialdistanceGetting:
          controller.showContentDialog(maps.toString());
          break;
      }
    });

  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("社交提醒".tr),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  onPressed: null,
                  child: Text('${'扫描间隔'.tr}:'),
                ),
              )),
              Expanded(child: intervalList.isNotEmpty?DropdownButton<String>(
                value: interval,
                items: intervalList.map<DropdownMenuItem<String>>((e) =>
                    DropdownMenuItem<String>(value:e,child: Text(e),)).toList(),
                onChanged: (value){
                  if(value != null){
                    setState(() {
                      interval = value;
                    });
                  }
                },
              ):Container()),
              Container(
                margin: const EdgeInsets.only(right: 10),
                child: Text('分钟'.tr),
              )
            ],
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  onPressed: null,
                  child: Text('${'扫描时长'.tr}:'),
                ),
              )),
              Expanded(child: durationList.isNotEmpty?DropdownButton<String>(
                value: duration,
                items: durationList.map<DropdownMenuItem<String>>((e) =>
                    DropdownMenuItem<String>(value:e,child: Text(e),)).toList(),
                onChanged: (value){
                  if(value != null){
                    setState(() {
                      duration = value;
                    });
                  }
                },
              ):Container()),
              Container(
                margin: const EdgeInsets.only(right: 10),
                child: Text('秒'.tr),
              )
            ],
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('设置'.tr),
                  onPressed: (){
                    controller.writeData(BleSDK.setSocialSetting(int.parse(interval),int.parse(duration),-60));
                  },
                ),
              )),
              Expanded(child: Container(
                margin: const EdgeInsets.only(right: 10),
                child: ElevatedButton(
                  child: Text('获取'.tr),
                  onPressed: (){
                    controller.writeData(BleSDK.getSocialSetting());
                  },
                ),
              ))
            ],
          ),

        ],
      ),
    );
  }

}
